﻿using System.Linq;
using DMHelper.Core.DM;
using DMHelper.Logic.DMLogic;
using DMHelper.Logic.Shared;
using DMHelper.Models.Interfaces;
using Data.Helpers;
using Data.Objects.Player;
using Data.Objects.Room;

namespace DMHelper.Models
{
    public class DmBaseModel : IDMModel
    {
        public IOrderedEnumerable<IPlayerCharacter> Players { get; set; }
        public IOrderedEnumerable<IRoom> Rooms { get; set; }
        public IRoom CurrentRoom { get; set; }
 
        public Options DmOptions { get; set; }

        public bool EncounterStarted { get; set; }

        public DmBaseModel()
        {
            GetPlayers();
            GetRooms();
        }

        private void GetPlayers()
        {
            var dmLogic = new DMLogic();
            Players = dmLogic.GetPlayers();
        }

        private void GetRooms()
        {
            var dmLogic = new DMLogic();
            Rooms = dmLogic.GetRooms();
            CurrentRoom = Rooms.First();
        }

        public IPlayerCharacter InitiativePlayer()
        {
            var initiativePlayer = Players.First(x => x.Active);

            return initiativePlayer;
        }

        public IOrderedEnumerable<IPlayerCharacter> GetInactivePlayers()
        {
            var initiativePlayer = InitiativePlayer();
            return Players.Where(x => x.Name != initiativePlayer.Name).OrderByDescending(x => x.Initiative);

        }

        public string PlayerXml(IPlayerCharacter player)
        {
            return XmlHelper.XmlSerializeToString(player);
        }

    }
}